<?php
namespace app\controller;

use app\BaseController;
use app\model\Article;
use app\model\ArticleCol;
use app\model\ArticleLab;
use app\model\Shops;
use app\model\User;
use app\model\UserInfoHobby;
use app\model\UserInfoIdentity;
use app\model\UserInfos;
use think\Request;
use think\facade\FileSystem;
use think\facade\Session;

class test extends BaseController{

    public function index(Request $request){

        $data = $request->param();

//        $file = $request->file('image');
//        // 上传到本地服务器
//        $savename = \think\facade\Filesystem::putFile( 'topic', $file);

        $file = $request->file('image');

        // 上传到本地服务器
        $savename = Filesystem::disk('photo') -> putFile('photo', $file, 'md5');;
        // 将图片路径保存
        $data['image']=$savename;
        $data['addTime']=date("Y-m-d H:i:s");
        $data['lastUpdTime']=null;
        $data['content']=nl2br($data['content']); // 将回车换行进行转换
        $data['uid']=Session::get("id");
        print_r( $data);

        $atData = [];
        // 将数组进行处理
        foreach ($data as $k=>$v){
            if($k=="arr"){
                continue;
            }
            if($k=="label"){
                continue;
            }
            // 对数据库文章的普通字段进行插入 user_article
            $atData["$k"]=["$v"];
        }

       // dump($atData);

        // 文章普通信息插入
//        $article = new Article();
//        $article->insertArticle($data);

        // 获取专栏分类的数据 user_articlecol
        $col = $data["arr"];
        //dump($col);
        $colList = [
            "springboot" => "",
            "ssm" => "",
            "Linux" =>  "",
            "MySql" => "",
            "ThinkPHP" => "",
            "Redis" => ""
        ];

        foreach ($col as $key1=>$value1){
           $colList["$value1"]=$value1;
        }
        dump($colList);

        // 插入数据
        $colAdd = new ArticleCol();
        $colAdd->insertArticleCol($colList,$data["uid"],$data["id"]);
//        $colAdd->saveArticleCol($colList,$data["uid"],$data["id"]);

        // 获取标签栏的 user_articlelab
        $label = $data["label"];


        $labelList = [
         "java"=>"",
         "python"=>"",
         "php"=>"",
         "C"=>"",
         "Go"=>"",
         "Hadoop"=>"",
        ];

        foreach ($label as $key2=>$value2){
            $labelList["$value2"]=$value2;
        }
        dump($labelList);
        $labAdd = new ArticleLab();
        $labAdd->insertArticleLab($labelList,$data["uid"],$data["id"]);
        //$labAdd->saveArticleLab($labelList,$data["uid"],$data["id"]);
    }


    public function test(){

            $uname = "zhangsan";
            $upwd = "123456";
            $model = new User();
            // 返回自增主键
            $id = $model->insertUser($uname,$upwd);
            // 在userinfo 和  liketag也进行插入
            dump($id);
            // 用户信息表插入
            $uinfo= new UserInfos();
            $uid = $uname.$id;
            dump($uid);
            // 爱好表插入
            $hb = new UserInfoHobby();
            $uinfo->insertUserInfo($id,$uname,$uid);
            $hb->insertHb($uid);
            //return "1";
    }
}